import * as API from '@/api/dataBoard'
import * as FileApi from '@/api/download'
import { ref, reactive, onBeforeMount, watch } from 'vue';
import * as formatLocalData from './formatLocalData';


const getMax = (_data: any) => {
  let total = 0
  _data.forEach((item: any) => {
    total += item.value
  })
  return total
}
const keys = ['weight', 'pressure', 'temp', 'frequency'];
export const getLocalData = async () => {
  if (localStorage.getItem('localData')) {
    return JSON.parse(localStorage.getItem('localData') as any);
  } else {
    const res = await FileApi.getJSON({});
    if (res) {
      return res;
    }
    return null;
  }
}
export default (key, isComp) => {
  const thisMonthData = reactive({
    water: {},
    gas: {},
    elec: {},
  })
  const lastMonthData = reactive({
    water: null,
    gas: null,
    elec: null,
  })
  const fuData = ref([])
  const production = reactive({
    rate: null,
    month: null,
    history: null,
  })
  const getThisMonth = async () => {
    const res = await API.getThisMonth()
    if (res) {
      thisMonthData.water = res.monthDosageVoMap['水']
      thisMonthData.elec = res.monthDosageVoMap['电']
      thisMonthData.gas = res.monthDosageVoMap['天燃气']
    }
  }
  const getLastMonth = async () => {
    const res = await API.getLastMonth()
    if (res) {
      lastMonthData.water = res['水']
      lastMonthData.elec = res['电']
    }
  }
  const getLastMonthGas = async () => {
    const res = await API.getLastMonthGas()
    if (res) {
      lastMonthData.gas = res
    }
  }
  const getFu = async () => {
    let res;
    if (isComp) {
      res = await formatLocalData.getFu();
    } else {
      res = await API.getFu();
    }
    if (res) {
      fuData.value = res.map((item) => {
        const obj = {
          ...item,
          targetedValues: []
        }
        item.targetedValues.forEach((target) => {
          const index = keys.indexOf(target.dateTypeCode)
          if (index!==-1) {
            obj.targetedValues[index] = target;
          }
        });
        return obj;
      })
    }
  }
  const getDateMonthRateCount = async () => {
    let res;
    if (isComp) {
      res = await formatLocalData.getDateMonthRateCount();
    } else {
      res = await API.getDateMonthRateCount();
    }
    if (res) {
      production.rate = res
    }
  }
  const getDateMonthCount = async () => {
    let res;
    if (isComp) {
      res = await formatLocalData.getDateMonthCount();
    } else {
      res = await API.getDateMonthCount();
    }
    if (res) {
      production.month = res
    }
  }
  const getDateHisCount = async () => {
    let res;
    if (isComp) {
      res = await formatLocalData.getDateHisCount();
    } else {
      res = await API.getDateHisCount();
    }
    if (res) {
      production.history = res
    }
  }
  onBeforeMount(() => {
    // getThisMonth()
    // getLastMonth()
    // getLastMonthGas()
    if (key == 'fu') {
      getFu()
    }
    if (key === 'utilizationRate') {
      getDateMonthRateCount()
    }
  
    if (key === 'production') {
      getDateMonthCount()
      getDateHisCount()
    }
    
  })
  return {
    thisMonthData,
    lastMonthData,
    fuData,
    production,
  }
}
